// pages/home/home.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        priceList: [ //金额表
            {
                no: 1,
                price: 100,
                litre: 0
            },
            {
                no: 2,
                price: 200,
                litre: 0
            },
            {
                no: 3,
                price: 300,
                litre: 0
            },
            {
                no: 4,
                price: 400,
                litre: 0
            },
            {
                no: 5,
                price: 500,
                litre: 0
            },
            {
                no: 6,
                price: 600,
                litre: 0
            },
            {
                no: 7,
                price: 700,
                litre: 0
            },
            {
                no: 8,
                price: 800,
                litre: 0
            },
        ],
        selected: [], //选择金额
        selected2: [], //确认金额
        unitCost: 7.700, //油价
        total: 0, //总金额
        cell: [
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        ], //占位
        isPay: 0,
        moneyValue: null,
        pays: ['IC卡支付', '银行卡支付', '互联网支付', '其他支付'],
        isPaied: 0
    },

    //选择金额
    choosePrice(e) {
        let p = e.currentTarget.dataset.p
        console.log(p);
        let arr = this.data.selected
        arr.push(p)
        console.log(arr);
        this.setData({
            selected: arr
        })

        // 计算体积
        let t = this.data.selected
        let u = this.data.unitCost
        for (let i = 0; i < t.length; i++) {
            t[i].litre = t[i].price / u
            t[i].litre = t[i].litre.toFixed(2) /*保留两位小数*/
        }

        this.setData({
            selected: t
        })
    },

    //确定金额
    confirmPrice(e) {
        console.log(e);
        let p = e.currentTarget.dataset.p
        console.log(p);
        let arr = this.data.selected2
        arr.push(p)
        console.log(arr);
        this.setData({
            selected2: arr
        })

        // 计算总金额
        let t = this.data.selected2
        let total = this.data.total
        for (let i = 0; i < t.length; i++) {
            total = t[i].price + total
        }
        console.log('total: ' + total);
        this.setData({
            total: total
        })
    },

    // 选择付款
    toPay() {
        let t = this.data.total
        if (t == 0) {
            wx.showToast({
                title: '未选择金额',
                icon: 'error'
            })
        } else {
            this.setData({
                isPay: 1
            })
            console.log("用户付款");
        }

    },

    inputMoney(e) {
        console.log("总金额为：" + e.detail.value);
    },

    pay(e) {
        console.log(e);
        let t = e.currentTarget.dataset.p
        wx.showToast({
            title: t,
            icon: 'loading'
        })
        this.setData({
            isPaied: 1
        })
    },

    onLoad() {
        this.setData({
            selected: [],
            selected2: [],
            isPay: 0,
            isPaied: 0,
            total:0
        })
    },

    re() {
        wx.showToast({
            title: '交易结束',
            icon: 'success'
        })
        this.onLoad()
    }
})